var delivererTable
var index
var url
layui.use(['table','form','layer','jquery'], function() {
    var table = layui.table
    var form = layui.form
    var layer = layui.layer
    var $ = layui.jquery

    // 初始table
    delivererTable = table.render({
        elem: "#delivererTable"
        , url: "/deliverer/findDelivererByloginId"
        , page: {limit: 5, limits: [5, 10, 15]}
        , cellMinWidth: 80
        , toolbar: '#toolbardeliverer'
        , cols: [[ //表头
            {type: 'checkbox', fixed: 'left'}
            , {field: 'delivererName', title: '姓名', fixed: 'center'}
            , {field: 'phone', title: '手机号', fixed: 'center'}
            , {field: 'loginId', title: '账号', fixed: 'center'}
            , {field: 'loginPwd', title: '密码', fixed: 'center'}
            , {field: 'state', title: '状态', fixed: 'center' ,templet: function (data) {
                    return data.state == 1 ? "派送中" : "空闲"
                }}
            , {field: 'createTime', title: '注册时间', fixed: 'center'}
            , {fixed: 'right', title: '操作', toolbar: '#bardeliverer', width: 250}
        ]]
    })
    // 查询
    form.on("submit(formDemo)", function (data) {
        delivererTable.reload({
            url: "/deliverer/findDelivererByloginId"
            , where: {
                'identity':data.field.identity
                ,'delivererName':data.field.delivererName
                ,'phone':data.field.phone
                ,'state':data.field.state

            }
            , page: {
                curr: 1
            }
        })
        return false;
    })
    // 头部工具栏添加事件
    table.on('toolbar(delivererTable)',function(obj) {
        if(obj.event=="delivererDelChk"){
            delivererDelChk(obj)
        }else if(obj.event=="delivererAdd"){
            delivererAdd();
        }
    })
    //监听行工具事件
    table.on('tool(delivererTable)', function(obj){
        var data = obj.data
        if(obj.event == "del"){
            del(data)
        }else if(obj.event=="edit"){
            delivererUpdate(data)
        }else if(obj.event=="reset"){
            reset(data)
        }else if(obj.event=="xiugai"){
            modifypwd(data)
        }
    })
    //重置密码
    function reset(data) {
        layer.confirm("您确定重置密码为123456么？", function (ind) {

            $.post("/adminDeliverer/resetDelivererPwd", {"identity": data.identity}, function (d) {
                layer.msg(d.msg)
                // 刷新table数据
                delivererTable.reload()

            })
            layer.close(ind)
        })
    }

    function del(data){
        layer.confirm("您确定要删除"+ data.delivererName +"派送员么？",function(ind){

            $.post("/adminDeliverer/delDeliverer", {"identity":data.identity} ,function(d) {
                layer.msg(d.msg)
                // 刷新table数据
                delivererTable.reload()

            })
            layer.close(ind)
        })

    }
    function delivererDelChk(obj) {
        var checkStatus=table.checkStatus(obj.config.id);
        var data=checkStatus.data
        if(data.length==0){
            layer.msg("请至少选择一个数据")
        }else {
            var ids=new Array()
            $.each(data,function (index,item) {
                ids.push(item.identity)
            })
            layer.confirm("你确定要删除"+data.length+"条数据么？",function (ind) {
                $.post("/adminDeliverer/delivererDelChk",{"ids":ids},function (r) {
                    layer.msg(r.msg)
                    delivererTable.reload()
                })
                layer.close(ind)
            })
        }
    }
    function delivererUpdate(data) {
        index =  layer.open({
            type:1
            , title:"修改派送员"
            , content:$("#addOrUpdateDiv")
            , area : ['440px', '440px']
            , success:function(layero, index) {
                form.val("dataFrm",data)
                $("#d1").hide()
                $("#loginId").addClass("layui-btn-disabled")
                $("#identity").addClass("layui-btn-disabled")
                url="/adminDeliverer/updateDeliverer"
            }
        })
    }



    function modifypwd() {
        // 弹出框
        index =  layer.open({
            type:1 //
            , title:"密码修改"
            , content:$("#modifyPwdform")
            , area : ['400px', '440px']
            , success:function(layero, index) {
                // 页面数据要清除
                $("#dataFrm")[0].reset()
                $("#d1").show()
                $("#loginId").removeClass("layui-btn-disabled")
                $("#identity").removeClass("layui-btn-disabled")
                url="/deliverer/modify"
            }
        })

    }
    // form表达提交
    form.on("submit(doSubmit1)",function() {
        layer.confirm("您确定要修改密码么？", function (ind) {
            $.post(url, $("#dataFrm").serialize(), function (data) {
                layer.msg(data.msg)
                layer.close(index)
                delivererTable.reload()
            })
            layer.close(ind)
        })

    })
})